function [moments,residuals] = Var1Threshold_GMM_moments_macro_spec(theta2,VarU_x2,...
    Cov10U_x2,Cov01U_x2,x2,macros,econAct,thresholdLevel,switch_h0,switch_hx)

% Extending the output from th regression filter with macro variables
nm          = size(macros,1);
[nx2,T]     = size(x2);
factors     = [macros;x2];
nx          = size(factors,1);
VarU        = zeros(nx,nx,T);
Cov10U      = zeros(nx,nx,T);
Cov01U      = zeros(nx,nx,T);
for t=1:T
    VarU(nm+1:nm+nx2,nm+1:nm+nx2,t)   = VarU_x2(:,:,t);
    Cov10U(nm+1:nm+nx2,nm+1:nm+nx2,t) = Cov10U_x2(:,:,t);
    Cov01U(nm+1:nm+nx2,nm+1:nm+nx2,t) = Cov01U_x2(:,:,t);
end

% We unfold theta2
nx            = size(factors,1);
[h0_1,h0_2,hx_1,hx_2,sigma,gama0,gamaz,gamax,sigmaz] = unfoldTheta2_threshold_macro(theta2,nx);
VarW          = sigma*sigma';      
regime_1      = econAct >= thresholdLevel; %a boom
regime_2      = econAct < thresholdLevel;  %a recression
     
% The moments conditions 
residuals   = zeros(nx,T);
residuals_1 = zeros(nx,T);
residuals_2 = zeros(nx,T);
residuals_z = zeros(1,T);
tmpx        = zeros(nx,nx,T);
tmpx_1      = zeros(nx,nx,T);
tmpx_2      = zeros(nx,nx,T);
tmpx_w      = zeros(nx,nx,T);
tmpz        = zeros(2+nx+1,T);
for t=1:T-1
   residuals(:,t+1) = factors(:,t+1) - (h0_1 + hx_1*factors(:,t))*regime_1(t,1)...
                                     - (h0_2 + hx_2*factors(:,t))*regime_2(t,1);
   tmpx(:,:,t+1)    = residuals(:,t+1)*factors(:,t)' ...
                        - Cov10U(:,:,t+1) + hx_1*VarU(:,:,t);          
   % For the process for x
   residuals_1(:,t+1) = residuals(:,t+1)*regime_1(t,1);
   residuals_2(:,t+1) = residuals(:,t+1)*regime_2(t,1);
   tmpx_1(:,:,t+1)    = residuals(:,t+1)*regime_1(t,1)*factors(:,t)' ...
                        - Cov10U(:,:,t+1)*regime_1(t,1) + hx_1*VarU(:,:,t)*regime_1(t,1);
   tmpx_2(:,:,t+1)    = residuals(:,t+1)*regime_2(t,1)*factors(:,t)' ...
                        - Cov10U(:,:,t+1)*regime_2(t,1) + hx_2*VarU(:,:,t)*regime_2(t,1);   
   omegaHat = VarU(:,:,t+1)...
       + hx_1*VarU(:,:,t)*regime_1(t,1)*hx_1' ...
       + hx_2*VarU(:,:,t)*regime_2(t,1)*hx_2' ...
       - Cov10U(:,:,t+1)*regime_1(t,1)*hx_1' - hx_1*Cov01U(:,:,t+1)*regime_1(t,1) ...
       - Cov10U(:,:,t+1)*regime_2(t,1)*hx_2' - hx_2*Cov01U(:,:,t+1)*regime_2(t,1);
   tmpx_w(:,:,t+1)    = residuals(:,t+1)*residuals(:,t+1)' - VarW - omegaHat;
   
   residuals_z(1,t+1) = econAct(t+1,1) - gama0 - gamaz*econAct(t,1) - gamax'*factors(:,t);
   % For the process for z
   tmpz(1,t+1)        = residuals_z(1,t+1);
   tmpz(2,t+1)        = residuals_z(1,t+1)*econAct(t,1);
   tmpz(2+1:2+nx,t+1) = reshape(residuals_z(1,t+1)*factors(:,t)'+gamax'*VarU(:,:,t),nx,1);
   tmpz(2+nx+1,t+1)   = residuals_z(1,t+1)^2 - sigmaz^2 - gamax'*VarU(:,:,t)*gamax;
end

% The moment conditions 
namesTheta2 = fieldnames(theta2);
numTheta2   = size(namesTheta2,1);
moments     = zeros(numTheta2,T-1);
idx         = 0;
for i=1:nx
    if switch_h0(i,1) == 1
        idx = idx + 1;
        moments(idx,:) = residuals_1(i,2:T);
        idx = idx + 1;
        moments(idx,:) = residuals_2(i,2:T);
    elseif switch_h0(i,1) == 0
        idx = idx + 1;
        moments(idx,:) = residuals(i,2:T);
    end
end
for i=1:nx
    for j=1:nx
        if switch_hx(i,j) == 1
            idx = idx + 1;
            moments(idx,:) = tmpx_1(i,j,2:T);
            idx = idx + 1;
            moments(idx,:) = tmpx_2(i,j,2:T);
        else
            idx = idx + 1;
            moments(idx,:) = tmpx(i,j,2:T);
        end
    end
end
for t=1:T-1
    moments(idx+1:idx+nx*(nx+1)/2,t) = vech(tmpx_w(:,:,t+1));
    moments(idx+nx*(nx+1)/2+1:idx+nx*(nx+1)/2+2+nx+1,t) = tmpz(:,t+1);
end

end